package com.lcredu.web;/**
 * @author 黎长容
 * @date 2025-07-05 17:03
 */

import com.lcredu.entity.Member;
import com.lcredu.service.MemberService;
import com.lcredu.service.impl.MemberServiceImpl;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet(name = "MemberRegisterServlet", urlPatterns = "/MemberRegisterServlet")
public class MemberRegisterServlet extends HttpServlet {
    private MemberService memberService = new MemberServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        //判断用户名是否可用
        if (memberService.getMemberByName(username)) {
            //如果可用
            Member member = new Member(null, username, password, email);
            System.out.println(member);
            if (memberService.savMember(member)) {
                //注册成功
                request.getRequestDispatcher("/views/member/register_ok.jsp").forward(request, response);
            } else {
                //注册失败
                request.getRequestDispatcher("/views/member/register_fail.jsp").forward(request, response);
            }
        } else {
            //不可用直接返回主页面
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
        }


    }
}